<template>
  <div>
    <el-button
      type="primary"
      plain
      icon="el-icon-plus"
      size="mini"
      @click="handleAdd"
      v-hasPermi="['system:hhjl:add']"
    >民政新增
    </el-button>

    <!-- 民政数据新增对话框 -->
    <el-dialog title="民政数据新增" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <div class="module-section">
          <div class="module-header">
            <i class="el-icon-office-building"></i>
            <span class="module-title">民政数据</span>
            <span class="module-remark">（所有带*字段均为必填）</span>
          </div>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="逝者编号" prop="szbh">
                <el-input v-model="form.szbh" placeholder="请输入逝者编号" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="火化时间" prop="hhsj">
                <el-date-picker
                  v-model="form.hhsj"
                  type="date"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="请选择火化时间"
                  style="width: 100%"
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="入馆时间" prop="rgsj">
                <el-date-picker
                  v-model="form.rgsj"
                  type="date"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="请选择入馆时间"
                  style="width: 100%"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="逝者姓名" prop="szxm">
                <el-input v-model="form.szxm" placeholder="请输入逝者姓名" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="性别" prop="xb">
                <el-select v-model="form.xb" placeholder="请选择性别" clearable style="width: 100%">
                  <el-option label="男" value="男" />
                  <el-option label="女" value="女" />
                  <el-option label="未知" value="未知" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年龄" prop="nl">
                <el-input v-model="form.nl" placeholder="请输入年龄" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="证件类型" prop="zjlx">
                <el-select v-model="form.zjlx" placeholder="请选择证件类型" clearable style="width: 100%">
                  <el-option label="居民身份证" value="居民身份证" />
                  <el-option label="护照" value="护照" />
                  <el-option label="军官证" value="军官证" />
                  <el-option label="其他" value="其他" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证件号码" prop="zjhm">
                <el-input v-model="form.zjhm" placeholder="请输入证件号码" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="户口所在地" prop="hkszd">
                <el-input v-model="form.hkszd" placeholder="请输入户口所在地" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生前住址" prop="szdz">
                <el-input v-model="form.szdz" placeholder="请输入生前住址" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="死亡日期" prop="swrq">
                <el-date-picker
                  v-model="form.swrq"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择死亡日期"
                  style="width: 100%"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="死亡原因" prop="swyy">
                <el-input v-model="form.swyy" placeholder="请输入死亡原因" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="承办人姓名" prop="cbrxm">
                <el-input v-model="form.cbrxm" placeholder="请输入承办人姓名" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="lxdh">
                <el-input v-model="form.lxdh" placeholder="请输入联系电话" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="与逝者关系" prop="gx">
                <el-select v-model="form.gx" placeholder="请选择与逝者关系" clearable style="width: 100%">
                  <el-option label="父子" value="父子" />
                  <el-option label="母子" value="母子" />
                  <el-option label="夫妻" value="夫妻" />
                  <el-option label="兄弟姐妹" value="兄弟姐妹" />
                  <el-option label="其他" value="其他" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="受理人员" prop="slry">
                <el-input v-model="form.slry" placeholder="请输入受理人员" clearable />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addHhjl } from '@/api/system/hhjl'

export default {
  name: 'Add',
  data() {
    return {
      open: false,
      form: {
        szbh: null,
        hhsj: null,
        rgsj: null,
        szxm: null,
        xb: null,
        nl: null,
        zjlx: null,
        zjhm: null,
        hkszd: null,
        szdz: null,
        swrq: null,
        swyy: null,
        cbrxm: null,
        lxdh: null,
        gx: null,
        slry: null
      },
      rules: {
        szbh: [{ required: true, message: '逝者编号不能为空', trigger: 'blur' }],
        hhsj: [{ required: true, message: '火化时间不能为空', trigger: 'change' }],
        rgsj: [{ required: true, message: '入馆时间不能为空', trigger: 'change' }],
        szxm: [{ required: true, message: '逝者姓名不能为空', trigger: 'blur' }],
        xb: [{ required: true, message: '性别不能为空', trigger: 'change' }],
        nl: [{ required: true, message: '年龄不能为空', trigger: 'blur' }],
        zjlx: [{ required: true, message: '证件类型不能为空', trigger: 'change' }],
        zjhm: [{ required: true, message: '证件号码不能为空', trigger: 'blur' }],
        hkszd: [{ required: true, message: '户口所在地不能为空', trigger: 'blur' }],
        // szdz: [{ required: true, message: '生前住址不能为空', trigger: 'blur' }],
        swrq: [{ required: true, message: '死亡日期不能为空', trigger: 'change' }],
        // swyy: [{ required: true, message: '死亡原因不能为空', trigger: 'blur' }],
        cbrxm: [{ required: true, message: '承办人姓名不能为空', trigger: 'blur' }],
        lxdh: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
        gx: [{ required: true, message: '与逝者关系不能为空', trigger: 'change' }],
        slry: [{ required: true, message: '受理人员不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleAdd() {
      this.reset()
      this.open = true
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        szbh: null,
        hhsj: null,
        rgsj: null,
        szxm: null,
        xb: null,
        nl: null,
        zjlx: null,
        zjhm: null,
        hkszd: null,
        szdz: null,
        swrq: null,
        swyy: null,
        cbrxm: null,
        lxdh: null,
        gx: null,
        slry: null
      }
      this.resetForm('form')
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          // 死亡日期校验
          if (this.form.swrq) {
            const deathDate = new Date(this.form.swrq)
            const entryTime = new Date(this.form.rgsj)
            const cremationTime = new Date(this.form.hhsj)

            if (deathDate >= entryTime) {
              this.$modal.msgError('死亡日期必须早于入馆时间')
              return
            }

            if (deathDate >= cremationTime) {
              this.$modal.msgError('死亡日期必须早于火化时间')
              return
            }
          }

          addHhjl(this.form).then(response => {
            this.$modal.msgSuccess('民政数据新增成功')
            this.open = false
            this.$emit('success')
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.module-section {
  margin-bottom: 24px;
  padding: 16px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  background: #f8f9fa;
}

.module-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 2px solid #409EFF;
}

.module-header i {
  font-size: 18px;
  margin-right: 8px;
  color: #409EFF;
}

.module-title {
  font-size: 16px;
  font-weight: 600;
  color: #409EFF;
  margin-right: 8px;
}

.module-remark {
  font-size: 12px;
  color: #f56c6c;
}
</style>
